
package.path = "../../../frameworks/global/?.lua;" .. package.path
package.path = "../../../frameworks/common/?.lua;../../../frameworks/mjgame/?.lua;" .. package.path

require("functions")
require("cardlib")

--- 游戏状态
GAME_STATUS = {}
GAME_STATUS.kReady = 1
GAME_STATUS.kDealCards = 2
GAME_STATUS.kTribute = 3
GAME_STATUS.kPlaying = 4
GAME_STATUS.kEnd = 5

-- 限发表情次数
GAME_STATUS.kStatusEmotion = 9
GAME_STATUS.kEmoRemain = 10

-- 游戏通知类型定义
NOTIFY_TYPE = {}    -- 自定义从1开始
NOTIFY_TYPE.kStatusGame     = GAME_STATUS.kStatusGame       -- 对应下面
NOTIFY_TYPE.kStatusEmotion  = GAME_STATUS.kStatusEmotion

-- 游戏牌型
CARDS_TYPE = {}
CARDS_TYPE.NONE = 0
CARDS_TYPE.SINGLE = 1
CARDS_TYPE.DOUBLE = 2
CARDS_TYPE.TRIPLE = 3
CARDS_TYPE.TRIPLE_WITH_DOUBLE = 4
CARDS_TYPE.SERIAL = 5
CARDS_TYPE.SERIAL_DOUBLE = 6
CARDS_TYPE.SERIAL_TRIPLE = 7
CARDS_TYPE.BOMB_4 = 8
CARDS_TYPE.BOMB_5 = 9
CARDS_TYPE.SERIAL_SAMECOLOR = 10
CARDS_TYPE.BOMB_6 = 12
CARDS_TYPE.BOMB_7 = 13
CARDS_TYPE.BOMB_8 = 14
CARDS_TYPE.BOMB_9 = 15
CARDS_TYPE.BOMB_10 = 16
CARDS_TYPE.BOMB_GOD = 20

-- 操作状态
PLAY_STATE    = {}
PLAY_STATE.STATE_NONE    = 0
PLAY_STATE.STATE_CHUPAI  = 1     --出牌
PLAY_STATE.STATE_YINGPAI = 2     --应牌

-- 操作状态
TRIBUTE_STATE    = {}
TRIBUTE_STATE.NONE    = 0
TRIBUTE_STATE.TRIBUTE  = 1         --进贡
TRIBUTE_STATE.BACK_TRIBUTE = 2     --还贡

--- @brief 调试时打印变量的值
--- @param data 要打印的字符串
--- @param [max_level] table要展开打印的计数，默认nil表示全部展开
--- @param [prefix] 用于在递归时传递缩进，该参数不供用户使用于
--- @ref http://dearymz.blog.163.com/blog/static/205657420089251655186/
function _table_dump(data, max_level, prefix)
    local ret = ""
    if type(prefix) ~= "string" then
        prefix = ""
    end
    if type(data) ~= "table" then
        ret = prefix .. tostring(data)
    else
        if max_level ~= 0 then
            local prefix_next = prefix .. "    "
            ret = ret .. prefix .. "{" .. "\n"
            for k, v in pairs(data) do
                --if not string.match(k,"^__.*") then
                    ret = ret .. prefix_next .. k .. " = "
                    if type(v) ~= "table" or (type(max_level) == "number" and max_level <= 1) then
                        ret = ret .. tostring(v) .. "\n"
                    else
                        ret = ret .. "\n"
                        if max_level == nil then
                            ret = ret .. _table_dump(v, nil, prefix_next) .. "\n"
                        else
                            ret = ret .. _table_dump(v, max_level - 1, prefix_next) .. "\n"
                        end
                    end
                --end
            end
            ret = ret .. prefix .. "}"
        end
    end
    return ret
end

table.dump = table.dump or function(data, max_level)
        return _table_dump(data, max_level or 5)
    end

function test_types()
    local rule = {
    }
    rule.types = {
        {
            id = CARDS_TYPE.SINGLE,
            name = "单张",
            code = "one",
            styles = {
                {each = 1, length = 1}
            }
        },
        {
            id = CARDS_TYPE.DOUBLE,
            name = "对子",
            code = "two",
            styles = {
                {each = 2, length = 1}
            }
        },
        {
            id = CARDS_TYPE.TRIPLE,
            name = "三张",
            code = "three",
            styles = {
                {each = 3, length = 1}
            }
        },
        {
            id = CARDS_TYPE.TRIPLE_WITH_DOUBLE,
            name = "三带二",
            code = "three2",
            styles = {
                {each = 3, length = 1},
                {each = 2, length = 1}
            }
        },
        {
            id = CARDS_TYPE.SERIAL,
            name = "顺子",
            code = "serial",
            styles = {
                {each = 1, length = 5, serial = true, serial_begin = "a", serial_end = "a"}
            }
        },
        {
            id = CARDS_TYPE.SERIAL_DOUBLE,
            name = "两连对",
            code = "serial2",
            styles = {
                {each = 2, length = 3, serial = true, serial_begin = "a", serial_end = "a"}
            }
        },
        {
            id = CARDS_TYPE.SERIAL_TRIPLE,
            name = "三顺",
            code = "serial3",
            styles = {
                {each = 3, length = 2, serial = true, serial_begin = "a", serial_end = "a"}
            }
        },
        {
            id = CARDS_TYPE.SERIAL_SAMECOLOR,
            name = "同花顺",
            code = "bombs",
            value = CARDS_TYPE.SERIAL_SAMECOLOR,
            styles = {
                {each = 1, length = 5, serial = true, samecolor = true, serial_begin = "a", serial_end = "a"}
            }
        },
        {
            id = CARDS_TYPE.BOMB_GOD,
            name = "王炸",
            code = "god",
            value = CARDS_TYPE.BOMB_GOD,
            styles = {
                {each = 2, length = 1, point = "xa"},
                {each = 2, length = 1, point = "xb"}
            }
        }
    }

    --炸弹
    for i = 4, 10 do
        table.insert(
            rule.types,
            {
                id = CARDS_TYPE["BOMB_" .. i],
                name = "炸弹",
                code = "bomb" .. i,
                value = CARDS_TYPE["BOMB_" .. i],
                styles = {
                    {each = i, length = 1}
                }
            }
        )
    end

    rule.baida = {
        cards = "h2",
        cantbe = "xa,xb",
    }
    lib = cardlib.initialize(rule)
    
    print(lib:card('s5'):name(),lib:card(30):name(),lib:card(38):name())
    print(lib:card('黑桃5'):name(),lib:card(30):name(),lib:card(38):name())

    local _set_maincard = function (maincard)
        local pps = {}
        local PP = lib.PP
        for i = PP.cp2, PP.cpK do
            if i ~= maincard:point() then
                table.insert(pps, i)
            end
        end
        if PP.cpA ~= maincard:point() then
            table.insert(pps, PP.cpA)
        end
        table.insert(pps, maincard:point())
        table.insert(pps, PP.cpXa)
        table.insert(pps, PP.cpXb)
        lib:set_point_value(pps)
        lib:set_baida(maincard, "xa,xb")
    end

    _set_maincard(lib:card('h5'))
    
    local tp = lib:cardlist({37,51,33,26,23,73,76,21,57,58,19,68,39,54,36,72,60,31,30,55,17,38,67,34,18,23,73});
    tp:sort(lib, "num", true)
    local mincard = tp:at(tp:count())
    local maxcard = tp:at(1)
    print('xxx1',tp:count(),tp:tostring())
    tp:remove(mincard)
    tp:refadd(maxcard)
    print('xxx2',tp:count(),tp:tostring())
    
    print("d4,d5,d6",lib:typeof("d4,d5,d6,h3"))

    tp:set('s7,d7,s10,h2,h2,d10,d8,h2,sj,dj')
    local p = lib:find_type_cards(tp,'bomb4')
    if p then
        print(tp:tostring(),'牌型',p.type.name,'表象牌:',p.out:tostring(),' 实际牌',p.pile:tostring())
    else
        print(tp:tostring(),'无牌型')
    end
    
    _set_maincard(lib:card('h5'))
    local xx = lib:cardlist("h8,h8,c8,s10,h10,d10,dk,dk,ca,hq,dq,h7,s4,c4,d4,s3,d3,c9,c9,s6,s6,h6,sj,hj,dj,h2")
    --xx = xx:sort(lib,"value")
    local xx2 = lib:cardlist()
    xx2:set("h8,h8,c8")
    xx:remove(xx2)
    xx2:set("s10,h10,d10")
    xx:remove(xx2)
    xx2:set("dk,dk")
    xx:remove(xx2)
    xx2:set("ca")
    xx:remove(xx2)
    xx2:set("hq,dq")
    xx:remove(xx2)
    xx2:set("ca")
    xx:remove(xx2)
    xx2:set("h7")
    xx:remove(xx2)
    xx2:set("s4,c4,d4,s3,d3")
    xx:remove(xx2)
    xx2:set("c9,c9,s6,s6,h6")
    xx:remove(xx2)
    xx2:set("sj,hj,dj")
    xx:remove(xx2)
    xx2:set("h2")
    xx:remove(xx2)
    xx2:set("c9,c9,s6,s6,h6")
    print("....",xx:has(xx2),xx:tostring())
    
    print("剩余的牌："..xx:tostring())

    local lastfind = 'h3,h3,h3,h3,h3'
    for i = 1, 1 do
        --print("查找比",lastfind and lastfind:tostring() or '','大的牌')
        lastfind = lib:find_great_cards(xx,lastfind or '')
        --lastfind = lib:find_great_cards(xx,'s5,s6,s7,s8,s9')
        if not lastfind then
            print("没找到")
            break 
        end
        print(lastfind.type.name or "未找到","实际牌型：",lastfind.out:tostring() or "","|",lastfind.pile:tostring() or "")
        lastfind = lastfind.out
    end
end

test_types()